<template>
  <div class="app-container">
    <!-- 搜索 -->
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="商品名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入商品名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品类型" prop="type">
        <el-select
          v-model="queryParams.type"
          style="width: 150px"
          placeholder="请选择商品类型"
          clearable
        >
          <el-option
            v-for="dict in type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="规格" prop="ruleId">
        <el-input
          v-model="queryParams.ruleId"
          placeholder="请输入规格"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品品牌" prop="brandId">
        <el-select
          v-model="queryParams.brandId"
          style="width: 150px"
          placeholder="请选择商品品牌"
          clearable
        >
          <el-option
            v-for="dict in type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="商品分类" prop="categoryId">
        <el-select
          v-model="queryParams.categoryId"
          style="width: 150px"
          placeholder="请选择商品分类"
          clearable
        >
          <el-option
            v-for="dict in type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="供应商" prop="supply">
        <el-input
          v-model="queryParams.supply"
          placeholder="请输入供应商"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="上架类型" prop="deployType">
        <el-select
          v-model="queryParams.deployType"
          style="width: 150px"
          placeholder="请选择上架类型"
          clearable
        >
          <el-option
            v-for="dict in deploy_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="上架时间" prop="deployTime">
        <el-date-picker
          clearable
          v-model="queryParams.deployTime"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择上架时间"
        >
        </el-date-picker>
      </el-form-item> -->
      <el-form-item label="下架类型" prop="undeployType">
        <el-select
          v-model="queryParams.undeployType"
          style="width: 150px"
          placeholder="请选择下架类型"
          clearable
        >
          <el-option
            v-for="dict in undeploy_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="下架时间" prop="undeployTime">
        <el-date-picker
          clearable
          v-model="queryParams.undeployTime"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择下架时间"
        >
        </el-date-picker>
      </el-form-item> -->
      <el-form-item label="商品单位" prop="unitId">
        <el-select
          v-model="queryParams.unitId"
          style="width: 150px"
          placeholder="请选择商品单位"
          clearable
        >
          <el-option
            v-for="dict in unit_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="标签" prop="tags">
        <el-select
          v-model="queryParams.tags"
          style="width: 150px"
          placeholder="请选择标签"
          clearable
        >
          <el-option
            v-for="dict in tags_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- 按钮 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['system:project:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:project:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:project:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['system:project:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>
    <!-- 列表 -->
    <el-table
      v-loading="loading"
      :data="projectList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键" align="center" prop="id" />
      <el-table-column label="商品名称" align="center" prop="name" />
      <el-table-column label="商品描述" align="center" prop="introduction" />
      <el-table-column label="主类型" align="center" prop="mianType" />
      <el-table-column label="父类型" align="center" prop="parentType" />
      <el-table-column label="商品类型" align="center" prop="type">
        <template #default="scope">
          <dict-tag :options="type" :value="scope.row.type" />
        </template>
      </el-table-column>
      <el-table-column label="商品图片" align="center" prop="image" width="100">
        <template #default="scope">
          <image-preview :src="scope.row.image" :width="50" :height="50" />
        </template>
      </el-table-column>
      <el-table-column
        label="商品轮播图"
        align="center"
        prop="carouselImages"
        width="100"
      >
        <template #default="scope">
          <image-preview
            :src="scope.row.carouselImages"
            :width="50"
            :height="50"
          />
        </template>
      </el-table-column>
      <el-table-column label="商品状态" align="center" prop="status" />
      <el-table-column label="规格" align="center" prop="ruleId" />
      <el-table-column label="商品品牌" align="center" prop="brandId">
        <template #default="scope">
          <dict-tag :options="type" :value="scope.row.brandId" />
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="商品分类" align="center" prop="categoryId">
        <template #default="scope">
          <dict-tag :options="type" :value="scope.row.categoryId" />
        </template>
      </el-table-column>
      <el-table-column label="供应商" align="center" prop="supply" />
      <el-table-column label="商品视频" align="center" prop="video" />
      <el-table-column label="上架类型" align="center" prop="deployType">
        <template #default="scope">
          <dict-tag :options="deploy_type" :value="scope.row.deployType" />
        </template>
      </el-table-column>
      <el-table-column
        label="上架时间"
        align="center"
        prop="deployTime"
        width="180"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.deployTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="下架类型" align="center" prop="undeployType">
        <template #default="scope">
          <dict-tag :options="undeploy_type" :value="scope.row.undeployType" />
        </template>
      </el-table-column>
      <el-table-column
        label="下架时间"
        align="center"
        prop="undeployTime"
        width="180"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.undeployTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="商品单位" align="center" prop="unitId">
        <template #default="scope">
          <dict-tag :options="unit_type" :value="scope.row.unitId" />
        </template>
      </el-table-column>
      <el-table-column label="标签" align="center" prop="tags">
        <template #default="scope">
          <dict-tag :options="tags_type" :value="scope.row.tags" />
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            link
            type="primary"
            icon="Edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:project:edit']"
            >修改</el-button
          >
          <el-button
            link
            type="primary"
            icon="Delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:project:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改商品列表对话框 弹框-->
    <el-dialog :title="title" v-model="open" width="1000px" append-to-body>
      <el-form ref="projectRef" :model="form" :rules="rules" label-width="80px">
        <el-tabs
          v-model="activeName"
          class="demo-tabs"
          @tab-click="handleClick"
        >
          <!-- 基础信息 -->
          <el-tab-pane label="基础信息" name="first">
            <div>
              <el-form-item label="商品名称" prop="name">
                <el-input v-model="form.name" placeholder="请输入商品名称" />
              </el-form-item>
              <el-form-item label="商品描述" prop="introduction">
                <el-input
                  v-model="form.introduction"
                  type="textarea"
                  placeholder="请输入内容"
                />
              </el-form-item>
              <el-form-item label="商品类型" prop="type">
                <el-radio-group v-model="form.type">
                  <el-radio
                    v-for="dict in type"
                    :key="dict.value"
                    :label="dict.value"
                    >{{ dict.label }}</el-radio
                  >
                </el-radio-group>
              </el-form-item>
              <el-form-item label="商品图片" prop="image">
                <image-upload v-model="form.image" />
              </el-form-item>
              <el-form-item label="商品轮播图" prop="carouselImages">
                <image-upload v-model="form.carouselImages" />
              </el-form-item>
              <!-- <el-form-item label="规格" prop="ruleId">
                <el-input v-model="form.ruleId" placeholder="请输入规格" />
              </el-form-item> -->
              <el-form-item label="商品品牌" prop="brandId">
                <el-select v-model="form.brandId" placeholder="请选择商品品牌">
                  <el-option
                    v-for="item in brandOptions"
                    :key="item.id"
                    :label="item.name"
                    :value="item.id"
                  ></el-option>
                </el-select>
              </el-form-item>
              <!-- <el-form-item label="备注">
                <editor v-model="form.remark" :min-height="192" />
              </el-form-item> -->
              <el-form-item label="商品分类" prop="categoryId">
                <el-select
                  v-model="form.categoryId"
                  placeholder="请选择商品分类"
                >
                  <el-option
                    v-for="item in categoryOptionArrs"
                    :key="item.key"
                    :label="item.value"
                    :value="item.key"
                  ></el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="供应商" prop="supply">
                <el-input v-model="form.supply" placeholder="请输入供应商" />
              </el-form-item>
              <el-form-item label="商品视频" prop="video">
                <file-upload
                  v-model="form.video"
                  :fileSize="50"
                  :fileType="['mp4', 'avi']"
                />
              </el-form-item>
              <el-form-item label="上架类型" prop="deployType">
                <el-select
                  v-model="form.deployType"
                  placeholder="请选择上架类型"
                >
                  <el-option
                    v-for="dict in deploy_type"
                    :key="dict.value"
                    :label="dict.label"
                    :value="parseInt(dict.value)"
                  ></el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="上架时间" prop="deployTime">
                <el-date-picker
                  clearable
                  v-model="form.deployTime"
                  type="date"
                  value-format="YYYY-MM-DD"
                  placeholder="请选择上架时间"
                >
                </el-date-picker>
              </el-form-item>
              <el-form-item label="下架类型" prop="undeployType">
                <el-select
                  v-model="form.undeployType"
                  placeholder="请选择下架类型"
                >
                  <el-option
                    v-for="dict in undeploy_type"
                    :key="dict.value"
                    :label="dict.label"
                    :value="parseInt(dict.value)"
                  ></el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="下架时间" prop="undeployTime">
                <el-date-picker
                  clearable
                  v-model="form.undeployTime"
                  type="date"
                  value-format="YYYY-MM-DD"
                  placeholder="请选择下架时间"
                >
                </el-date-picker>
              </el-form-item>
              <el-form-item label="商品单位" prop="unitId">
                <el-select v-model="form.unitId" placeholder="请选择商品单位">
                  <el-option
                    v-for="dict in unit_type"
                    :key="dict.value"
                    :label="dict.label"
                    :value="parseInt(dict.value)"
                  ></el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="标签" prop="tags">
                <el-select v-model="form.tags" placeholder="请选择标签">
                  <el-option
                    v-for="dict in tags_type"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </div>
          </el-tab-pane>
          <!-- 库存规格 -->
          <el-tab-pane label="库存规格" name="second">
            <div>
              <!-- 下拉框  选择规格 -->
              <el-row>
                <el-form-item label="选择规格" prop="ruleId">
                  <el-select
                    v-model="form.ruleId"
                    style="width: 150px"
                    placeholder="请选择规格"
                  >
                    <el-option
                      v-for="item in ruleList"
                      :key="item.id"
                      :label="item.name"
                      :value="parseInt(item.id)"
                    ></el-option>
                  </el-select>
                  &emsp;
                  <el-button
                    type="primary"
                    @click="selectRuleAttrListByRuleId(form.ruleId)"
                    >确定</el-button
                  >
                </el-form-item>
              </el-row>
              <!-- 动态  规格tags -->
              <el-row>
                <el-card
                  v-for="item in ruleContentList"
                  :key="item.name"
                  style="width: 600px; height: 80%"
                >
                  <el-row>
                    <span style="font-size: x-small">{{ item.name }}</span>
                    <div class="flex gap-2">
                      <el-tag
                        v-for="tag in item.attrValue.split(',')"
                        :key="tag"
                        closable
                        :disable-transitions="false"
                        @close="handleClose(tag)"
                      >
                        {{ tag }}
                      </el-tag>
                      <el-input
                        v-if="inputVisible"
                        ref="InputRef"
                        v-model="inputValue"
                        class="w-20"
                        size="small"
                        @keyup.enter="handleInputConfirm"
                        @blur="handleInputConfirm"
                      />
                      <el-button
                        v-else
                        class="button-new-tag"
                        size="small"
                        @click="showInput"
                        style="width: 130px"
                      >
                        + New Tag
                      </el-button>
                    </div>
                  </el-row>
                </el-card>
              </el-row>
              <el-divider content-position="center">商品SKU信息</el-divider>
              <el-row :gutter="10" class="mb8">
                <el-col :span="1.5">
                  <el-button
                    type="primary"
                    icon="Plus"
                    @click="handleAddProjectSkuInfo"
                    >添加</el-button
                  >
                </el-col>
                <el-col :span="1.5">
                  <el-button
                    type="danger"
                    icon="Delete"
                    @click="handleDeleteProjectSkuInfo"
                    >删除</el-button
                  >
                </el-col>
              </el-row>
              <el-table
                :data="projectSkuInfoList"
                :row-class-name="rowProjectSkuInfoIndex"
                @selection-change="handleProjectSkuInfoSelectionChange"
                ref="projectSkuInfo"
              >
                <el-table-column type="selection" width="50" align="center" />
                <el-table-column
                  label="序号"
                  align="center"
                  prop="index"
                  width="50"
                />
                <!-- 动态table标题的编写 -->
                <el-table-column
                  v-for="title in titleList"
                  :prop="title.prop"
                  :label="title.label"
                  :key="title.prop"
                >
                  <template #default="scope">
                    <!-- 回显正常格式的标题 -->
                    <span v-if="title.prop.indexOf('prop') > -1">{{
                      scope.row[title.prop]
                    }}</span>
                    <!-- 数值类型下拉框 -->
                    <el-input-number
                      v-else-if="['stock', 'price'].indexOf(title.prop) > -1"
                      v-model="scope.row[title.prop]"
                      :precision="2"
                      :step="0.01"
                    ></el-input-number>
                    <!-- 图片显示 -->
                    <image-upload
                      v-else-if="['image'].indexOf(title.prop) > -1"
                      v-model="scope.row[title.prop]"
                      :limit="1"
                      :is-show-tip="false"
                    ></image-upload>
                  </template>
                </el-table-column>

                <el-table-column label="商品库存" prop="stock" width="150">
                  <template #default="scope">
                    <el-input
                      v-model="scope.row.stock"
                      placeholder="请输入商品库存"
                    />
                  </template>
                </el-table-column>
                <el-table-column label="商品价格" prop="price" width="150">
                  <template #default="scope">
                    <el-input
                      v-model="scope.row.price"
                      placeholder="请输入商品价格"
                    />
                  </template>
                </el-table-column>
                <el-table-column
                  label="商品销售价格"
                  prop="salePrice"
                  width="150"
                >
                  <template #default="scope">
                    <el-input
                      v-model="scope.row.salePrice"
                      placeholder="请输入商品销售价格"
                    />
                  </template>
                </el-table-column>
              </el-table>
            </div>
          </el-tab-pane>
          <!-- 商品详情 -->
          <el-tab-pane label="商品详情" name="third">
            <el-form-item label="商品详情">
              <editor v-model="form.remark" :min-height="192" />
            </el-form-item>
          </el-tab-pane>
          <!-- 商品关联 -->
          <el-tab-pane label="商品关联" name="fourth">
            <el-transfer v-model="value" :data="transferData" />
          </el-tab-pane>
        </el-tabs>

        <template #footer>
          <div class="dialog-footer">
            <el-button type="primary" @click="submitForm">确 定</el-button>
            <el-button @click="cancel">取 消</el-button>
          </div>
        </template>
      </el-form>
    </el-dialog>
  </div>
</template>

<script setup name="Project">
import {
  listProject,
  getProject,
  delProject,
  addProject,
  updateProject,
} from "@/api/system/project";
import { selectCategoryListByParentId } from "@/api/system/category";

import { listBrand } from "@/api/system/brand";
import { listRule } from "@/api/system/rule";
import { listRuleAttr } from "@/api/system/ruleAttr";

const titleLabel = ref([]);
const titleProp = ref([]);

//动态标题表格
const titleList = ref([
  // { label: "规格图片", prop: "image" },
  // { label: "商品库存", prop: "stock" },
  // { label: "商品价格", prop: "price" },
]);

//动态  tags
const inputValue = ref("");
const dynamicTags = ref(["Tag 1", "Tag 2", "Tag 3"]);
const inputVisible = ref(false);
const InputRef = ref();

function handleClose(tag) {
  dynamicTags.value.splice(dynamicTags.value.indexOf(tag), 1);
}

function showInput() {
  inputVisible.value = true;
  nextTick(() => {
    InputRef.value.focus();
  });
}

const ruleAttrInfoList = ref([]);

//获取tag数组getTagArray
// function getTagArray(tagRuleAttr) {
//   let result = ref([]);
//   result.value = tagRuleAttr.split(",");
//   return result;
// }

function handleInputConfirm() {
  if (inputValue.value) {
    dynamicTags.value.push(inputValue.value);
  }
  inputVisible.value = false;
  inputValue.value = "";
}

const ruleList = ref([]);
const ruleContentList = ref([]);
/** 查询商品规格列表 */
function getRuleList() {
  loading.value = true;
  listRule(null).then((response) => {
    ruleList.value = response.rows;
    console.log(ruleList.value);
  });
}

//库存规格  确定按钮 事件
function selectRuleAttrListByRuleId(ruleId) {
  console.log("根据ruleId<" + ruleId + ">获取ruleAttr");
  let p = {};
  p.ruleId = ruleId;
  listRuleAttr(p).then((response) => {
    ruleContentList.value = response.rows;
    // console.log(ruleContentList.value);
    response.rows.forEach((item) => {
      console.log(item);
      titleList.value.push(item.name);
    });
    // titleList.value.label = ruleContentList.value.name;
  });

  // ruleContentList.value.forEach((item, index) => {

  //   let title = {};
  //   title.name = item.name;
  //   console.log(index, item);
  //   titleList.value.push(item.name);
  // });
  //
}

const brandOptions = ref([]);
/** 查询商品品牌列表 */
function getBrandList() {
  loading.value = true;
  listBrand(queryParams.value).then((response) => {
    brandOptions.value = response.rows;
  });
}

const categoryOptionArrs = ref([]);
/** 查询品类信息列表 */
function getCategoryList() {
  loading.value = true;
  selectCategoryListByParentId().then((response) => {
    debugger;
    console.log(response.data);

    categoryOptionArrs.value = response.data;
    console.log(categoryOptionArrs.value);
    // categoryOptions.value = proxy.handleTree(response.data, "id", "parentId");
  });
}

//穿梭框
const generateData = () => {
  const transferData = [];
  for (let i = 1; i <= 15; i++) {
    transferData.push({
      key: i,
      label: `Option ${i}`,
      disabled: i % 4 === 0,
    });
  }
  return transferData;
};

const transferData = ref(generateData());
const value = ref([]);

// tab 切换
const activeName = ref("first");

function handleClick(tab, event) {
  console.log(tab, event);
}

const { proxy } = getCurrentInstance();
const { undeploy_type, tags_type, unit_type, deploy_type, type } =
  proxy.useDict(
    "undeploy_type",
    "tags_type",
    "unit_type",
    "deploy_type",
    "type"
  );

const projectList = ref([]);
const projectSkuInfoList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const checkedProjectSkuInfo = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    name: null,
    introduction: null,
    mianType: null,
    parentType: null,
    type: null,
    image: null,
    carouselImages: null,
    status: null,
    ruleId: null,
    brandId: null,
    categoryId: null,
    supply: null,
    video: null,
    deployType: null,
    deployTime: null,
    undeployType: null,
    undeployTime: null,
    unitId: null,
    tags: null,
  },
  rules: {
    name: [{ required: true, message: "商品名称不能为空", trigger: "blur" }],
    type: [{ required: true, message: "商品类型不能为空", trigger: "change" }],
    image: [{ required: true, message: "商品图片不能为空", trigger: "blur" }],
    carouselImages: [
      { required: true, message: "商品轮播图不能为空", trigger: "blur" },
    ],
    categoryId: [
      { required: true, message: "商品分类不能为空", trigger: "change" },
    ],
    unitId: [
      { required: true, message: "商品单位不能为空", trigger: "change" },
    ],
  },
});

const { queryParams, form, rules } = toRefs(data);

/** 查询商品列表列表 */
function getList() {
  loading.value = true;
  listProject(queryParams.value).then((response) => {
    projectList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    name: null,
    introduction: null,
    mianType: null,
    parentType: null,
    type: null,
    image: null,
    carouselImages: null,
    status: null,
    ruleId: null,
    brandId: null,
    remark: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    categoryId: null,
    supply: null,
    video: null,
    deployType: null,
    deployTime: null,
    undeployType: null,
    undeployTime: null,
    unitId: null,
    tags: null,
  };
  projectSkuInfoList.value = [];
  proxy.resetForm("projectRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加商品列表";

  //库存规格
  getRuleList();
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value;
  getProject(_id).then((response) => {
    form.value = response.data;
    projectSkuInfoList.value = response.data.projectSkuInfoList;
    open.value = true;
    title.value = "修改商品列表";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["projectRef"].validate((valid) => {
    if (valid) {
      form.value.projectSkuInfoList = projectSkuInfoList.value;
      if (form.value.id != null) {
        updateProject(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addProject(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal
    .confirm('是否确认删除商品列表编号为"' + _ids + '"的数据项？')
    .then(function () {
      return delProject(_ids);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}

/** 商品SKU序号 */
function rowProjectSkuInfoIndex({ row, rowIndex }) {
  row.index = rowIndex + 1;
}

/** 商品SKU添加按钮操作 */
function handleAddProjectSkuInfo() {
  let obj = {};
  obj.sku = "";
  obj.stock = "";
  obj.price = "";
  obj.salePrice = "";
  obj.image = "";
  obj.remark = "";
  projectSkuInfoList.value.push(obj);
}

/** 商品SKU删除按钮操作 */
function handleDeleteProjectSkuInfo() {
  if (checkedProjectSkuInfo.value.length == 0) {
    proxy.$modal.msgError("请先选择要删除的商品SKU数据");
  } else {
    const projectSkuInfos = projectSkuInfoList.value;
    const checkedProjectSkuInfos = checkedProjectSkuInfo.value;
    projectSkuInfoList.value = projectSkuInfos.filter(function (item) {
      return checkedProjectSkuInfos.indexOf(item.index) == -1;
    });
  }
}

/** 复选框选中数据 */
function handleProjectSkuInfoSelectionChange(selection) {
  checkedProjectSkuInfo.value = selection.map((item) => item.index);
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    "system/project/export",
    {
      ...queryParams.value,
    },
    `project_${new Date().getTime()}.xlsx`
  );
}

getList();
getCategoryList();
getBrandList();
</script>

<style>
.demo-tabs > .el-tabs__content {
  padding: 32px;
  color: #6b778c;
  font-size: 32px;
  font-weight: 600;
}
</style>
